{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f50556f7",
   "metadata": {},
   "source": [
    "# 什么是随机数？\n",
    "随机数并不意味着每次都有不同的数字。随机意味着无法在逻辑上预测的事物。\n",
    "\n",
    "# 伪随机和真随机\n",
    "计算机在程序上工作，程序是权威的指令集。因此，这意味着必须有某种算法来生成随机数。\n",
    "\n",
    "如果存在生成随机数的程序，则可以预测它，因此它就不是真正的随机数。\n",
    "\n",
    "通过生成算法生成的随机数称为伪随机数。\n",
    "\n",
    "我们可以生成真正的随机数吗？\n",
    "\n",
    "是的。为了在我们的计算机上生成一个真正的随机数，我们需要从某个外部来源获取随机数据。外部来源通常是我们的击键、鼠标移动、网络数据等。\n",
    "\n",
    "我们不需要真正的随机数，除非它与安全性（例如加密密钥）有关或应用的基础是随机性（例如数字轮盘赌轮）。\n",
    "\n",
    "在本教程中，我们将使用伪随机数。\n",
    "\n",
    "# 生成随机数\n",
    "NumPy 提供了 random 模块来处理随机数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "df82dc01",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "47\n"
     ]
    }
   ],
   "source": [
    "from numpy import random\n",
    "\n",
    "x = random.randint(100)\n",
    "\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "da21474b",
   "metadata": {},
   "source": [
    "# 生成随机浮点\n",
    "random 模块的 rand() 方法返回 0 到 1 之间的随机浮点数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "6971d1cb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.016353967353135657\n"
     ]
    }
   ],
   "source": [
    "x = random.rand()\n",
    "\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "583927f6",
   "metadata": {},
   "source": [
    "# 生成随机数组\n",
    "在 NumPy 中，我们可以使用上例中的两种方法来创建随机数组。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "ed3361c7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[43 44 40 81 92]\n"
     ]
    }
   ],
   "source": [
    "x=random.randint(100, size=(5))\n",
    "\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "3c81513e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0 35 23 45 44]\n",
      " [49 14 16 54 31]\n",
      " [17 99 68 24 81]]\n"
     ]
    }
   ],
   "source": [
    "x = random.randint(100, size=(3, 5))\n",
    "\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "e4d15a56",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.48241711 0.53170904 0.94423911 0.24477121 0.23571381]\n"
     ]
    }
   ],
   "source": [
    "x = random.rand(5)\n",
    "\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "1f5db424",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0.10981282 0.57160414 0.06147226 0.70220986 0.03510524]\n",
      " [0.96498988 0.32441849 0.33314616 0.99045954 0.47291332]\n",
      " [0.62723475 0.49340048 0.41114823 0.39487083 0.36584198]]\n"
     ]
    }
   ],
   "source": [
    "x = random.rand(3, 5)\n",
    "\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c6901e48",
   "metadata": {},
   "source": [
    "# 从数组生成随机数\n",
    "choice() 方法使您可以基于值数组生成随机值。\n",
    "\n",
    "choice() 方法将数组作为参数，并随机返回其中一个值。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "8100db09",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "9\n"
     ]
    }
   ],
   "source": [
    "x = random.choice([3, 5, 7, 9])\n",
    "\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "48a3caf5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[5 5 3 5 7]\n",
      " [9 3 5 7 9]\n",
      " [5 7 5 7 7]]\n"
     ]
    }
   ],
   "source": [
    "# 生成由数组参数（3、5、7、9）中的值组成的二维数组\n",
    "x = random.choice([3, 5, 7, 9], size=(3, 5))\n",
    "\n",
    "print(x)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.10"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
